package t327.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import t327.mapper.StyleNumberMapper;
import t327.pojo.OrderForm;
import t327.pojo.StyleNumber;
import t327.pojo.until.Result;
import t327.service.iter.IStyleNumberService;

import java.util.List;
import java.util.Random;

@Service
public class StyleNumberServiceImpl implements IStyleNumberService {
    @Autowired
    private StyleNumberMapper styleNumberMapper;
    @Override
    public List<StyleNumber> getStyleNumberList() {
        return styleNumberMapper.getStyleNumberList();
    }
    @Override
    public int OrderOrderStyleRel(Long orderId ,Long styleNumberId) {
        return styleNumberMapper.OrderOrderStyleRel(orderId,styleNumberId);
    }

    @Override
    public int UpdateStyleNumber(Long id) {
        return styleNumberMapper.UpdateStyleNumber(id);
    }

    @Override
    public Result addStyleNumber(StyleNumber styleNumber) {
        Random random = new Random();// 创建一个 Random 对象
        long timestamp = System.currentTimeMillis();// 获取当前时间戳
        String result=null;
        int styleNumber1=0;
        /*循环生成款号（KH+6位随机数）
        * 循环的同时并查询款号是否已存在，不存在就return，反之则继续循环
         * */
        do {
            styleNumber1=0;
            int randomNumber = random.nextInt(90) + 10;// 生成一个10到90之间的随机数
            result = String.valueOf(randomNumber + timestamp);
            styleNumber1= styleNumberMapper.queryByStyleNum(result);
        }while (styleNumber1!=0);
        styleNumber.setStyleNumber(result);
        int i = styleNumberMapper.addStyleNumber(styleNumber);
        String num=styleNumber.getStyleNumber();
        if (i>0){
            return new Result(200,"添加成功",true,num);
        }else {
            return new Result(202,"添加失败",false,null);
        }
    }

    @Override
    public int updateStyleNumberById(Long styleNumberId) {
        return styleNumberMapper.updateStyleNumberById(styleNumberId);
    }

    @Override
    public int delStyleNumById(Long styleNumberId) {
        return styleNumberMapper.delStyleNumById(styleNumberId);
    }

    public List<OrderForm> getOrderFromBayId() {
        return styleNumberMapper.getOrderFromBayId();
    }
    @Override
    public List<StyleNumber> getStyleNumber() {
        return styleNumberMapper.getStyleNumber();
    }

    @Override
    public int orderFromCount() {
        return styleNumberMapper.orderFromCount();
    }

}
